home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / commentsbase.py < prev    next >
Text File  |  2008-08-27  |  4KB  |  116 lines

  1. #UNDER DEVELOPMENT - CURRENTLY NOT WORKING
  2. from lphelper import change_obj, user, LateBindingProperty
  3. from attachmentsbase import LPAttachments, LPAttachment
  4.  
  5. import exceptions
  6.  
  7.  
  8. class LPComment(object):
  9.     def __init__(self, subject=None, text=None, attachment=None):
  10.         self.subject = subject
  11.         self.text = text
  12.         self.__attachments = set()
  13.         if attachment is None:
  14.             attachment = set()
  15.         if isinstance(attachment, LPAttachment):
  16.             attachment = set((attachment,))
  17.         if attachment:
  18.             x = [a for a in attachment if not isinstance(a, LPAttachment)]
  19.             if x:
  20.                 e = dict((k, "type is '%s'" %type(k)) for k in x)
  21.                 raise exceptions.PythonLaunchpadBugsValueError(values=e,
  22.                         msg="Files attached to a Comment have to be instance of LPAttachment")
  23.             self.__attachments = attachment
  24.         self.__nr = id(self) #  set unique number since comment is not attached to comments
  25.         self.__user = None
  26.         self.__date = None
  27.         
  28.     def set_attr(self, **kwargs):
  29.         self.__nr = kwargs.get("nr", self.__nr)
  30.         self.__user = kwargs.get("user", self.__user)
  31.         self.__date = kwargs.get("date", self.__date)
  32.         
  33.     def __repr__(self):
  34.         if self.__nr and self.__user and self.__date:
  35.             return "<Comment #%s by %s on %s>" %(self.__nr, self.__user, self.__date)
  36.         else:
  37.             return "<Comment 'unknown'>"
  38.         
  39.     @property
  40.     def number(self):
  41.         return self.__nr
  42.     
  43.     @property
  44.     def user(self):
  45.         return self.__user
  46.     
  47.     @property
  48.     def date(self):
  49.         '''TODO: return Date-object ??'''
  50.         return self.__date
  51.         
  52.         
  53.     def get_attachments(self):
  54.         return self.__attachments
  55.         
  56.     def set_attachments(self, attachment):
  57.         if isinstance(attachment, Attachment):
  58.             assert not self.attachments, "Impossible to add more then one file to a comment"
  59.             self.__attachments.add(attachment)
  60.         else:
  61.             raise TypeError, ""
  62.     attachments = property(get_attachments, set_attachments, doc="attachment added to a comment")
  63.  
  64.  
  65. class LPComments(list):
  66.     
  67.     def __init__(self, comments=None, url=None):
  68.         self._cache = []
  69.         self.__url = url
  70.         self.parsed = False
  71.         if comments is None:
  72.             comments = list()
  73.         if comments:
  74.             x = [c for c in comments if not isinstance(c, LPComment)]
  75.             if x:
  76.                 e = dict((k, "type is '%s'" %type(k)) for k in x)
  77.                 raise exceptions.PythonLaunchpadBugsValueError(values=e,
  78.                         msg="All Comments have to be instance of LPComment")
  79.         list.__init__(self, comments)
  80.         
  81.     def __repr__(self):
  82.         #TODO: add bugnumber, parse from self.__url
  83.         return "<Commentslist>"
  84.         
  85.     def __str__(self):
  86.         x = (len(self) > 1 and "[%s]") or "%s"
  87.         return x %",".join(str(i) for i in self)
  88.         
  89.     def new(self, *args, **kwargs):
  90.         raise NotImplementedError, 'this method must be implemented by a concrete subclass'
  91.       
  92.     def parse(self):
  93.         pass
  94.         
  95.     def add(self, comment):
  96.         if isinstance(comment, LPComment):
  97.             self.append(comment)
  98.         else:
  99.             #TODO: raise TypeError
  100.             raise IOError, "'comment' must be an instance of 'LPComment'"
  101.     
  102.     @property
  103.     def changed(self):
  104.         changed = []
  105.         save = self[:]
  106.         while True:
  107.             if self._cache == save:
  108.                 return changed
  109.             else:
  110.                 x = save.pop()
  111.                 changed.insert(0,change_obj(x, action="added"))
  112.             
  113.     def commit(self, force_changes=False, ignore_lp_errors=True):
  114.         raise NotImplementedError, 'this method must be implemented by a concrete subclass'
  115.  
  116.